//
//  VistaInfoLista.m
//  ProyectoMoviTiendas
//
//  Created by Daniel Paez on 3/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VistaInfoLista.h"
#import "CeldaInfoLista.h"
#import "VistaInfoItemLista.h"
#import "Item.h"
#import "MoviTiendas.h"


@interface VistaInfoLista ()
{
    Item * itemSeleccionado;
}

@end

@implementation VistaInfoLista
@synthesize lista;
@synthesize lblPrecioTotal;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    lblPrecioTotal.text = @"0";
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

- (void)viewDidUnload
{
    [self setLblPrecioTotal:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    int cantidad = 0;

        cantidad = [lista.items count];
    
    return cantidad;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        static NSString *CellIdentifier = @"CeldaInfoLista";
        CeldaInfoLista *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell)
        {
            cell = [[CeldaInfoLista alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            
        }
        Item * item = [lista.items objectAtIndex:[indexPath row]];
        
        cell.item = item;
        cell.lblNombreItem.text = item.producto.nombre;
        cell.lblTienda.text = item.tienda.nombre;
        cell.lblCantidad.text = [NSString stringWithFormat:@"%d",item.cantidad];
        cell.lblPrecioUnitario.text = [NSString stringWithFormat:@"%d",item.precioUnitario];
        cell.lblSubtotal.text = [NSString stringWithFormat:@"%d",item.precioUnitario*item.cantidad];
        cell.imagen.image = item.producto.imagen;
        
        lblPrecioTotal.text = [NSString stringWithFormat:@"%d",lista.precioTotal];
        return cell;
    

}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // Delete the row from the data source
        
        [lista eliminarItem:[indexPath row]];
       
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        lblPrecioTotal.text = [NSString stringWithFormat:@"%d",lista.precioTotal];

    }   
    /*
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    } */  
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([lista.items count]) 
    {
        itemSeleccionado = [lista.items objectAtIndex:[indexPath row]];
        [self performSegueWithIdentifier:@"verInfoItemLista" sender:self];
    }
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"verInfoItemLista"]) 
    {
        VistaInfoItemLista * vista = [segue destinationViewController];
        vista.item = itemSeleccionado;
        vista.lista = lista;
    }
    
}
- (IBAction)agregarItemsACarritos:(id)sender 
{
    MoviTiendas * principal = [MoviTiendas darInstancia];
    for (Item *itemParaAgregar in lista.items) 
    {
        
        [principal.cliente agregarItemACarrito:[Item copiarItem:itemParaAgregar]]; 
        
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Lista agregada" message:@"La lista ha sido agregada a los carritos de compras" delegate:nil cancelButtonTitle:@"Aceptar" otherButtonTitles: nil];    
    
    [alert show];
    
}



@end
